// 用户数据模块

import {
  getUserInfo,
  loginByUsername,
  logout,
  refreshToken,
} from "@/api/login";
import { getStore, setStore } from "@/libs/storage";
import { encryption } from "@/libs/utils";

export const state = {
  userInfo: null,
  access_token:
    getStore({
      name: "access_token",
    }) || "",
  refresh_token:
    getStore({
      name: "refresh_token",
    }) || "",
};

export const getters = {
  userInfo: (state) => state.userInfo,
  access_token: (state) => state.access_token,
  refresh_token: (state) => state.refresh_token,
};

export const actions = {
  // user login
  Login({ commit }, userInfo) {
    const user = encryption({
      data: userInfo,
      key: "thanks,pig4cloud",
      param: ["password"],
    });
    return new Promise((resolve, reject) => {
      loginByUsername(user.username, user.password)
        .then((response) => {
          const { access_token, refresh_token } = response;
          commit("SET_ACCESS_TOKEN", access_token);
          commit("SET_REFRESH_TOKEN", refresh_token);
          resolve();
        })
        .catch((error) => {
          reject(error);
        });
    });
  },
  // user logout
  LogOut({ commit }) {
    return new Promise((resolve, reject) => {
      logout()
        .then(() => {
          commit("SET_USER_INFO", null);
          commit("SET_ACCESS_TOKEN", "");
          commit("SET_REFRESH_TOKEN", "");
          resolve();
        })
        .catch((error) => {
          reject(error);
        });
    });
  },
  // get user info
  GetUserInfo({ commit }) {
    return new Promise((resolve, reject) => {
      getUserInfo()
        .then((res) => {
          const data = res.data || null;
          commit("SET_USER_INFO", data);
          setStore({
            name: "userInfo",
            content: data,
          });
          resolve();
        })
        .catch((error) => {
          reject(error);
        });
    });
  },
  // refresh token
  RefreshToken({ commit, state }) {
    return new Promise((resolve, reject) => {
      refreshToken(state.refresh_token)
        .then((response) => {
          const data = response.data;
          commit("SET_ACCESS_TOKEN", data.access_token);
          commit("SET_REFRESH_TOKEN", data.refresh_token);
          resolve();
        })
        .catch((error) => {
          reject(error);
        });
    });
  },
  // remove token
  RemoveToken({ commit }) {
    return new Promise((resolve) => {
      commit("SET_USER_INFO", null);
      commit("SET_ACCESS_TOKEN", "");
      commit("SET_REFRESH_TOKEN", "");
      resolve();
    });
  },
};

export const mutations = {
  SET_TOKEN(state, token) {
    state.token = token;
    uni.setStorageSync("token", token);
  },
  SET_USERID(state, userId) {
    state.userId = userId;
    uni.setStorageSync("userId", userId);
  },

  SET_USER_INFO: (state, userInfo) => {
    state.userInfo = userInfo;
  },
  SET_ACCESS_TOKEN: (state, access_token) => {
    state.access_token = access_token;
    setStore({
      name: "access_token",
      content: state.access_token,
    });
  },
  SET_REFRESH_TOKEN: (state, refresh_token) => {
    state.refresh_token = refresh_token;
    setStore({
      name: "refresh_token",
      content: state.refresh_token,
    });
  },
};
